package com.wyd.System.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyd.System.Bean.Institute;
import com.wyd.System.Service.InstituteService;
import com.wyd.System.Vo.InstituteVo;
import com.wyd.handler.BusinessException;
import com.wyd.response.Result;
import com.wyd.response.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 文永达
 * @since 2020-12-25
 */
@RestController
@RequestMapping("/System/institute")
public class InstituteController {
    @Autowired
    private InstituteService instituteService;

    @ApiOperation(value = "查询学院及人数",notes = "从学院表中分组查询学生人数")
    @GetMapping("/findInstitute")
    public Result findInstitute(){
        List<Institute> institutes=instituteService.findInstitute();
        if(institutes.size()==0){
            throw new BusinessException(ResultCode.INSTITUTE_NOT_FOUND_EXCEPTION.getCode(),
                    ResultCode.INSTITUTE_NOT_FOUND_EXCEPTION.getMessage());
        }
        return Result.ok().data("institutes",institutes);
    }


    @ApiOperation("分页查询学院列表")
    @PostMapping("/findInstituteList")
    public Result findInstituteList(@RequestParam(required = true,defaultValue = "1")Integer current,
                                    @RequestParam(required = true,defaultValue = "7")Integer size,
                                    @RequestBody InstituteVo instituteVo){
        Page<Institute> page = new Page<>(current,size);
        QueryWrapper<Institute> wrapper = getWrapper(instituteVo);
        IPage<Institute> institutePage = instituteService.findInstituteList(page,wrapper);
        long total = institutePage.getTotal();
        List<Institute> records = institutePage.getRecords();
        return Result.ok().data("total",total).data("records",records);

    }

    private QueryWrapper<Institute> getWrapper(InstituteVo instituteVo){
        QueryWrapper<Institute> queryWrapper = new QueryWrapper<>();
        if(instituteVo!=null){
            if(!StringUtils.isEmpty(instituteVo.getInstituteName())){
                queryWrapper.eq("institute_name",instituteVo.getInstituteName());
            }
            if(!StringUtils.isEmpty(instituteVo.getInstCOUNT())){
                queryWrapper.eq("instCOUNT",instituteVo.getInstCOUNT());
            }
        }
        return queryWrapper;
    }

    @ApiOperation(value = "添加学院",notes = "添加学院")
    @PostMapping("/addInstitute")
    public Result addInstitute(@RequestBody Institute institute){
        try {
            instituteService.addInstitute(institute);
            return Result.ok();
        }catch (Exception e){
            //打印日志
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "删除学院",notes = "删除学院信息")
    @DeleteMapping("/delete/{id}")
    public Result deleteInstituteById(@PathVariable Integer id){
        try {
            instituteService.deleteInstituteById(id);
            return Result.ok();
        }catch (Exception e){
            //打印日志
            e.printStackTrace();
            return Result.error();
        }
    }
}

